"""
 -*- coding: utf-8-*-
 @File: page_add_member.py
 @Author: chen
 @time: 2023/09/10/ 18:44
"""
import time

import allure
from selenium.webdriver.common.by import By

from hogwartsHomework.homework_web_L4.dev.page_base import PageBase
from hogwartsHomework.homework_web_L4.dev.page_login import LoginPage
from hogwartsHomework.homework_web_L4.tools.record_log import logger
from hogwartsHomework.homework_web_L4.tools.save_data import save_screenshot, save_page_source


# 添加成员页面
class AddMemberPage(PageBase):
    def click_cancel(self):
        """点击取消按钮，回到首页"""

        self.wait_until_clickable((By.XPATH,'//*[text()="取消"]'))
        __button_cancel = """document.querySelector('.qui_btn.ww_btn.js_btn_cancel').click()"""
        with allure.step("点击取消按钮"):
            logger.info("点击取消按钮")
            self.execute_js(__button_cancel)
        from dev.page_contact_list import ContactListPage
        logger.info("回到通讯录页面")
        return ContactListPage(self.driver)

    def input_param(self,
                    name="钟离",
                    account="19912345678",
                    email="zhongli@163.com",
                    phone_num="19925622362"):
        __input_name = (By.NAME, 'username')
        __input_account = (By.NAME, "acctid")
        __input_email = (By.NAME, "alias")
        __input_phone = (By.NAME, "mobile")
        # __button_save = (By.XPATH, '//*[text()="保存"]')
        self.wait_until_visibility(__input_name)
        with allure.step(f"输入姓名：{name}"):
            logger.info(f"输入姓名：{name}")
            self.do_input(__input_name, name)
        with allure.step(f"输入账号：{account}"):
            logger.info(f"输入账号：{account}")
            self.do_input(__input_account, account)
        with allure.step(f"输入手机号：{phone_num}"):
            logger.info(f"输入手机号：{phone_num}")
            self.do_input(__input_phone, phone_num)
        with allure.step(f"输入邮箱：{email}"):
            logger.info(f"输入邮箱：{email}")
            self.do_input(__input_email, email)
        save_screenshot(self.driver)
        save_page_source(self.driver)

        return AddMemberPage(self.driver)

    def click_save(self):
        self.wait_until_clickable((By.XPATH, '//*[text()="保存"]'))
        __btn_save = """document.querySelector('.qui_btn.ww_btn.js_btn_save').click()"""
        # 4、点击保存
        with allure.step("点击保存按钮"):
            logger.info("点击保存按钮")
            self.execute_js(__btn_save)
        from dev.page_contact_list import ContactListPage
        logger.info("进入通讯录页面")
        time.sleep(3)
        return ContactListPage(self.driver)


if __name__ == '__main__':
    cur_page = LoginPage().goto_login().login().goto_add_member()
    cur_page.input_param().click_save()
